<?php
namespace app\admin\controller;
use think\Db;
use think\Request;
use clt\Form;
class Quarter extends Common{
    // protected  $dao,$fields;
    public function _initialize()
    {
       /* parent::_initialize();
        $this->moduleid = $this->mod[MODULE_NAME];
        $this->dao = db(MODULE_NAME);
        $fields = F($this->moduleid.'_Field');
        foreach($fields as $key => $res){
            $res['setup']=string2array($res['setup']);
            $this->fields[$key]=$res;
        }
        unset($fields);
        unset($res);
        $this->assign ('fields',$this->fields);*/
    }



    /**
     * 季度报告 开启的
     * @return [type] [description]
     */
    public function report()
    {
        if (request() -> isPost()) {
            $key      = input('post.key', '', 'trim');
            $quarter  = input('post.quarter', '', 'trim');
            $year  = input('post.year', '', 'trim');
            $d_name   = input('post.d_name', '', 'trim');
            $page     = input('page') ?input('page') :1;
            $pageSize = input('limit')?input('limit'):config('pageSize');

            $quarter=db('wqs_quarter')->where(array('open'=>1))->find();
            if(!$quarter)return $result = ['code'=>0,'msg'=>'获取成功!','data'=>[],'count'=>0,'rel'=>1];

            $list  = db('wqs_zuser z')
                  -> join('quarter_report qr', 'z.id=qr.uid','left')
                  -> join('wqs_quarter q', 'q.id=qr.qid','left')

                  -> join('wqs_dept d', 'd.id=z.dept_id','left')
                  -> join('wqs_classify c', 'c.id=d.classify_id','left')                  


                 // -> where(['z.dept_id'=>session('dept_id')])
                 -> where(['z.name'=>['like',"%$key%"]])
                 // -> where(['q.id'=>$quarter['id']])
                 -> where(['z.is_test'=>1])
                 -> where(['d.name'=>['like',"%$d_name%"]])
                 -> field('z.*,d.name d_name,c.name c_name,qr.qid,qr.status status')
                 -> paginate(array('list_rows'=>$pageSize,'page'=>$page))
                 -> toArray();

            $info = $list['data'];
            foreach ($info as $key => $value) {

              $info[$key]['leadcheck']=$this->getLeader($value['id']);
              $info[$key]['quarter']=$quarter['quarter'];
       $info[$key]['year']=$quarter['year'];
            }
            //状态


            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$info,'count'=>$list['total'],'rel'=>1];
        }
        return $this->fetch();
    }





    /**
     * 添加报告
     */
    public function add(){
        $data   = input('param.');
        $list  = db('wqs_zuser z')
                  -> join('wqs_dept d', 'd.id=z.dept_id')
                  -> where(['z.is_test'=>['eq',1]])
                 -> field('z.*')
                 ->find();

        $this->assign('list',$list);         

        $this->assign ( 'title', '添加报告' );
        return $this->fetch();
    }    



    /**
     * 季度报告
     */
    public function edit()
    {
        if (request() -> isPost()) {
            $data   = input('param.');
            $data['status'] = 1;

            if($id=$data['id'])
            {
              unset($data['id']);
              $result = db('quarter_report')->where(array('id'=>$id)) -> update($data);
            }else{
              $result = db('quarter_report') -> insert($data);
            }

            if ($result) {
                $result = ['code' => 1, 'msg'  => '添加成功', 'url'  => url('report')];
            } else {
                $result = ['code' => 0, 'msg'  => '添加失败', 'url'  => url('edit')];
            }
            return $result;
        }


        if($result=db('wqs_quarter')->where(array('open'=>1))->find()){
            $qid=$result['id'];
            $this -> assign('qid', $qid);
        }else{
            $this->error('季度报告关闭');
        }
       

      
        $uid   = input('param.id');
        $user=db('wqs_zuser z')
              -> join('wqs_dept d', 'd.id=z.dept_id','left')
              ->where(array('z.id'=>$uid))
              -> field('z.*,d.name d_name')
              ->find();
        $this -> assign('user', $user);


        //报告
        $report=db('quarter_report')->where(array('uid'=>$uid,'qid'=>$qid))->find();
        $this -> assign('report', $report);


        $this -> assign('uid', $uid);
        return view();
    }



    /**
     * 历史报告
     * @return [type] [description]
     */
    public function history()
    {
       if (request() -> isPost()) {
            $key      = input('post.key', '', 'trim');
            $quarter  = input('post.quarter', '', 'trim');
      $year  = input('post.year', '', 'trim');
            $d_name   = input('post.d_name', '', 'trim');
            $page     = input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');

            $list  = db('quarter_report qr')
                  -> join('wqs_zuser z', 'z.id=qr.uid','left')
                  -> join('wqs_quarter q', 'q.id=qr.qid','left')

                  -> join('wqs_dept d', 'd.id=z.dept_id','left')
                  -> join('wqs_classify c', 'c.id=d.classify_id','left')                  


                 // -> where(['z.dept_id'=>session('dept_id')])
                 -> where(['z.name'=>['like',"%$key%"]])
                 -> where(['q.quarter'=>['like',"%$quarter%"]])
     -> where(['q.year'=>['like',"%$year%"]])
                 -> where(['d.name'=>['like',"%$d_name%"]])
                 -> field('z.*,d.name d_name,c.name c_name,qr.qid,q.quarter,q.year,qr.status status')
                 -> paginate(array('list_rows'=>$pageSize,'page'=>$page))
                 -> toArray();

            $info = $list['data'];
            foreach ($info as $key => $value) {
              $info[$key]['leadcheck']=$this->getLeader($value['id']);
            }
            //状态


            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$info,'count'=>$list['total'],'rel'=>1];
        }
        return $this->fetch();
    }


    /**
     * 历史报告-查看
     */
    public function view()
    {
        if (request() -> isPost()) {
            $data   = input('param.');
            $data['status'] = 1;

            if($id=$data['id'])
            {
              unset($data['id']);
              $result = db('quarter_report')->where(array('id'=>$id)) -> update($data);
            }else{
              $result = db('quarter_report') -> insert($data);
            }

            if ($result) {
                $result = ['code' => 1, 'msg'  => '添加成功', 'url'  => url('report')];
            } else {
                $result = ['code' => 0, 'msg'  => '添加失败', 'url'  => url('edit')];
            }
            return $result;
        }


        if($wqs_quarter=db('wqs_quarter')->where(array('open'=>1))->find()){
            $qid=$wqs_quarter['id'];
            $this -> assign('qid', $qid);
        }else{
            $this->error('季度报告关闭');
        }
       

      
        $uid   = input('param.id');
        $user=db('wqs_zuser z')
              -> join('wqs_dept d', 'd.id=z.dept_id','left')
              ->where(array('z.id'=>$uid))
              -> field('z.*,d.name d_name')
              ->find();
        $this -> assign('user', $user);


        //报告
        $report=db('quarter_report')->where(array('uid'=>$uid,'qid'=>$qid))->find();
        $this -> assign('report', $report);

        $this->assign('four_results_img', explode(",", $report['four_results_img']));
        $this->assign('train_detail_img', explode(",", $report['train_detail_img']));
        $this->assign('clean_work_img', explode(",", $report['clean_work_img']));
        $this->assign('dislike_work_img', explode(",", $report['dislike_work_img']));
        $this->assign('ability_job_img', explode(",", $report['ability_job_img']));
        $this->assign('rewards_punish_img', explode(",", $report['rewards_punish_img']));
        $this->assign('name',$wqs_quarter['name']);
        $this->assign('quarter', $wqs_quarter['quarter']);
   $this->assign('year', $wqs_quarter['year']);

        $this -> assign('uid', $uid);
        return view();
    }
 


    public function listDel(){
        $id = input('post.id');
        $model = $this->dao;
        $model->where(array('id'=>$id))->delete();//转入回收站
        return ['code'=>1,'msg'=>'删除成功！'];
    }
    public function delAll(){
        $map['id'] =array('in',input('param.ids/a'));
        $model = $this->dao;
        $model->where($map)->delete();
        $result['code'] = 1;
        $result['msg'] = '删除成功！';
        $result['url'] = url('index',array('catid'=>input('post.catid')));
        return $result;
    }




   private function getLeader($id){
      // echo $id;
      $leader=db('wqs_zuser')->where(['leaderCheck' => ['exp', 'is not null']])->field('name,leaderCheck')->select();
      foreach ($leader as $key => $value) {
        if(in_array($id,explode(",", $value['leaderCheck'])))return $value['name'];
      }
      
    }
    
}